package gogogo.liwushuo.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import gogogo.liwushuo.R;
import gogogo.liwushuo.adapter.RecyclerViewAdapter;
import gogogo.liwushuo.bean.CommentBean;
import gogogo.liwushuo.constant.Resource;
import gogogo.liwushuo.util.OKHttpUtils;

/**
 * Created by chenjun on 2016/7/19.
 */
public class SubHomeFragment extends Fragment {

    @InjectView(R.id.recyclerview)
    RecyclerView recyclerview;
    private Activity mContext;
    private int index;
    private CommentBean commentBean;
    private List<CommentBean.DataBean.ItemsBean> itemsBeen;
    private RecyclerViewAdapter adapter;
    private LinearLayoutManager layout;

    public static SubHomeFragment newInstance(int index) {
        Bundle b = new Bundle();
        b.putInt("index", index);
        SubHomeFragment fragment = new SubHomeFragment();
        fragment.setArguments(b);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        Bundle mData = getArguments();
        if (mData != null) {
            index = mData.getInt("index");
        }
        getJsonData();

    }

    private void setRecyclerVAdapter() {
        layout = new LinearLayoutManager(mContext);
        recyclerview.setLayoutManager(layout);
        recyclerview.setAdapter(adapter);
        if (index == 0) {

        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.home_recycler, container, false);
        ButterKnife.inject(this, view);
        setRecyclerVAdapter();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public void getJsonData() {
        OKHttpUtils okHttpUtils = OKHttpUtils.newInstance(mContext);
        adapter = new RecyclerViewAdapter(mContext, itemsBeen);
        okHttpUtils.getAsyncMainData(Resource.URL_HOME[index], new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                Log.i(Resource.TAG, "onResponse: "+result);
                commentBean = new Gson().fromJson(result, CommentBean.class);
                itemsBeen = commentBean.getData().getItems();
                adapter.setList(itemsBeen);

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
